<?php
include "resources/header.php";
include "resources/non-search-pages.php";
//Begin Content
if(isset($_GET['id'])) {
	$profile_id = $_GET['id'];
}else {
	$profile_id = $_SESSION['auth'];
}
?>
<script src='http://malsup.github.com/jquery.form.js'></script>
<script>
$(document).ready(function() {
	var options = {
		beforeSend: function()
		{
			$("#message").html("");
		},
		success: function()
		{
		},
		complete: function(response)
		{
			$('#overlay').fadeOut();
			$('#upload-profile-picture-form').fadeOut();
			$('#profile-photo').attr('src', 'images/' + response.responseText);
		},
		error: function()
		{
			$("#message").html("<font color='red'> ERROR: unable to upload files</font>");
		}
	};
     $("#upload-photo-form").ajaxForm(options);
 
});
</script>
<?php
include "class/User.class.php";
$show = true;
$visited_regions[] = null;
$visited_regions_names[] = null;
include("inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php");
$mysqli = new mysqli($host, $user, $password, $database);
if(mysqli_connect_errno()) {
	printf("Connect failed: %s\n", mysqli_connect_error());
	exit();
}

$sql = "SELECT user_id FROM users WHERE user_id='$profile_id'";
if($result = $mysqli->query($sql)) {
	if($result->num_rows != 1) {
		$show = false;
	}
}
$sql = "SELECT region_id, region_name, region_lat, region_long, country_name FROM user_region NATURAL LEFT JOIN regions NATURAL LEFT JOIN countries WHERE user_id = '$profile_id'";
if($result = $mysqli->query($sql)) {
	if($result->num_rows >= 1) {
		while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
			extract($row);
			$visited_regions[] = "['$region_name, $country_name', $region_lat, $region_long, $region_id],";
			$visited_regions_names[] = "<span class='text-map-countries'>$region_name<span id='x'>&#10006</span><span id='region_id'>$region_id</span></span>";
		}
	}
	$result->close();
}
$mysqli->close();

	if($show) {
		echo '<div id="profile-bar">';
			$user = new User();
			$user->get_information($profile_id);
			echo "<p>";
			echo "<div id='profile-photo-container'>";
			if($profile_id == $_SESSION['auth']) {
				echo "<div id='profile-photo-click-change'>";
				echo "</div>";
			}
				$photo = $user->get_photo($profile_id);
				echo "<img src='images/$photo' id='profile-photo' />";
			echo '</div>';
			echo '</p>';
			echo '<p>';
			echo "<span class='line'><span>Username</span>" . $user->get_username() . "</span></p>";
			echo '<span class="line"><span>Country</span>';
				if($user->get_origin() == null) {
					echo "-";
				} else {
					echo $user->get_origin();
				}
			echo '</span></p>';
		echo '</div>';
	} else {
		echo '<div id="no-such-user">';
			echo '<div id="exclamation">!</div>';
			echo 'No such user is found';
		echo '</div>';
	}	
?>
<div id="places-been-to">
	<?php
	if($show) {
		echo '<div id="map"></div>';
	}
	?>
		<script type="text/javascript">
			var map;
			var bounds;
			var existingMarkers = new Array();
			var worldCenter = new google.maps.LatLng(0,0);
			var markers = [
				<?php
				foreach($visited_regions as $region) {
					echo "$region";
				};
				?>
			];
			function initialize() {
				var mapOptions = {
						zoom: 2,
						maxZoom: 15,
						minZoom: 2,
						mapTypeId: google.maps.MapTypeId.ROADMAP,
						center: worldCenter,
					};
				map = new google.maps.Map(document.getElementById("map"),
					mapOptions);
				
				//These area marks the locations plotted
				var infowindow = new google.maps.InfoWindow(), 
					marker, i;
				bounds = new google.maps.LatLngBounds();
				for (i = 0; i < markers.length; i++) {  
					var pos = new google.maps.LatLng(markers[i][1], markers[i][2]);
					bounds.extend(pos);
					marker = new google.maps.Marker({
						position: pos,
						map: map
					});
					marker.set("id", markers[i][3]);
					var infowindow = new google.maps.InfoWindow(), 
					marker, i;
					google.maps.event.addListener(marker, 'click', (function(marker, i){
						return function() {
							infowindow.setContent(markers[i][0]);
							infowindow.open(map, marker);
						}
					})(marker, i));
					map.fitBounds(bounds);
					existingMarkers.push(marker);
				}
					
				google.maps.event.addListener(marker,'click',function() {
					infowindow.open(map,marker);
				});
			}
			google.maps.event.addDomListener(window, 'load', initialize);
		</script>
	<div id="text-map">
		<?php
		if($visited_regions != null) {
			foreach($visited_regions_names as $name) {
				//add a region_id so that users can delete places that they have added
				//have jquery fade out when click on the cross
				echo $name;
			};
		}
		if(isset($_GET['id']) && $_GET['id'] == $_SESSION['auth'] || !isset($_GET['id'])) {
			echo '<div class="tiny-nl-info"><span id="add-location-button">[+Location]</span></div>';
		}
		?>
	</div>
	<?php
	if(isset($_GET['id']) && $_GET['id'] == $_SESSION['auth'] || !isset($_GET['id'])) {
		echo '<div id="add-location-bar">';
			echo '<div id="select-region-loader">';
				echo '<img src="images/ajax-loader.gif">';
			echo '</div>';
			echo '<form id="add_location_form" onsubmit="add_visited();return false;">';
				echo '<select name="country" id="country">';
					echo '<option value="0">Select Country</option>';
					include("inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php");
					$mysqli = new mysqli($host, $user, $password, $database);
					if (mysqli_connect_errno()) {
						printf("Connect failed: %s\n", mysqli_connect_error());
						exit();
					}
					$sql = "SELECT country_name, country_id FROM countries ORDER BY country_name";
					if($result = $mysqli->query($sql)) {
						if($result->num_rows >= 1) {
							while($row = $result->fetch_array(MYSQLI_ASSOC)) {
								extract($row);
								echo "<option value='$country_id'>$country_name</option>";
							}
						} 
						$result->close();
					}
					$mysqli->close();
				echo '</select>';
				echo '<select name="region" id="region" disabled>';
					echo '<option value="0">Select Region</option>';
				echo '</select>';
				echo '<input type="submit" class="submit" value="Add Location" disabled>';
			echo '</form>';
		echo '</div>';
	}
?>
</div>
<?php
//End Content
include "resources/footer.php";
?>